/*
 * @lc app=leetcode.cn id=661 lang=cpp
 *
 * [661] 图片平滑器
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<vector<int>> imageSmoother(vector<vector<int>> &img)
    {
        const int m = img.size();
        const int n = img[0].size();
        vector<vector<int>> ans(
            m, vector<int>(n, 0));
        int ud[] = {-1, 1, -1, 1, 0, 0, -1, 1};
        int lr[] = {-1, 1, 1, -1, -1, 1, 0, 0};
        for (size_t i = 0; i < m; i++)
        {
            for (size_t j = 0; j < n; j++)
            {
                int tmp = img[i][j];
                int cnt = 1;
                for (int k = 0; k < 8; k++)
                {
                    int nx = i + ud[k];
                    int ny = j + lr[k];
                    if (nx >= 0 && nx < m && ny >= 0 && ny < n)
                    {
                        cnt++;
                        tmp += img[nx][ny];
                    }
                }
                ans[i][j] = tmp / cnt;
            }
        }
        return ans;
    }
};
// @lc code=end
